package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyCommend;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("collects")
public class CollectController {
    @Autowired
    private ITravelService travelService;

    @Autowired
    private IDailyService dailyService;
    @Autowired
    private IDailyCommendService commendService;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IUserService userService;

    /**
     * 获取该游记是否收藏的状态
     * @param userId    用户的id
     * @param travelId  游记的id
     * @return
     */
    @GetMapping("travel")
    public Object getTravelAndStatus(Long userId,Long travelId){
        Map<String,Object> travelAndStatus = travelService.getTravelAndStatus(userId,travelId);
        return travelAndStatus;
    }
    /**
     * 获取该攻略是否收藏的状态
     * @param userId    用户的id
     * @param strategyId  攻略的id
     * @return
     */
    @GetMapping("strategy")
    public Object getStrategyStatus(Long userId,Long strategyId){
        int  status = strategyService.getStrategyStatus(userId,strategyId);
        return status;
    }
    @GetMapping("daily")
    public Object getDailyStatus(Long userId,Long dailyId){
        Integer  status = dailyService.getDailyStatus(userId,dailyId);
        return status;
    }

    /**
     * 查询该攻略被收藏的个数
     * @param strategyId 攻略id
     * @return
     */
    @GetMapping("strategyCount")
    public Integer getStrategyCount(Long strategyId){
        return strategyService.getStrategyCount(strategyId);
    }

    /**
     * 获取通过用户id获取收藏的游记
     * @param userId
     * @return
     */
    @GetMapping("travels")
    public List<Travel> getTravels(Long userId){
        return travelService.getTravelsByUserId(userId);
    }

    /**
     * 通过用户id,获取收藏的攻略
     * @param userId
     * @return
     */
    @GetMapping("strategys")
    public List<Strategy> getStrategys(Long userId){
        return strategyService.getStrategysByUserId(userId);
    }

    @GetMapping("dailyCommends")
    public List<DailyCommend> getDailys(Long userId){
        return commendService.getDailyCommendsByUserId(userId);
    }



    /**
     * 添加游记和用户的关联
     * @param userId 用户id
     * @param travelId 游记id
     * @param status 状态
     * @return
     */
    @PostMapping("/travels")
    public JSONResult insertRelationWithTravelId(Long userId,Long travelId,Long status){
        JSONResult result = new JSONResult();
        try {
            userService.insertRelationWithTravelId(userId,travelId,status);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 添加攻略和用户的关联
     * @param userId 用户id
     * @param strategyId 攻略id
     * @param status 状态
     * @return
     */
    @PostMapping("/strategys")
    public JSONResult insertRelationWithStrategyId(Long userId,Long strategyId,Long status){
        JSONResult result = new JSONResult();
        try {
            userService.insertRelationWithStrategyId(userId,strategyId,status);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 添加日报和中间表的关联
     * @param userId 用户id
     * @param dailyId 日报id
     * @param status 状态
     * @return
     */
    @PostMapping("/dailys")
    public JSONResult insertRelationWithdailyId(Long userId,Long dailyId,Long status){
        JSONResult result = new JSONResult();
        try {
            userService.insertRelationWithDailyId(userId,dailyId,status);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }



    /**
     * 破坏游记和用户之间的关系
     * @param userId 用户id
     * @param travelId 游记id
     * @return
     */
    @DeleteMapping("/travels")
    public JSONResult deleteRelationWithTravelId(Long userId,Long travelId){
        JSONResult result = new JSONResult();
        try {
            userService.deleteRelationWithTravelId(userId,travelId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 破坏攻略和用户之间的关系
     * @param userId 用户id
     * @param strategyId 攻略id
     * @return
     */
    @DeleteMapping("/strategys")
    public JSONResult deleteRelationWithStrategyId(Long userId,Long strategyId){
        JSONResult result = new JSONResult();
        try {
            userService.deleteRelationWithStrategyId(userId,strategyId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 破坏用户和日报之间的关系
     * @param userId 用户id
     * @param dailyId 日报id
     * @return
     */
    @DeleteMapping("/dailys")
    public JSONResult deleteRelationWithDailyId(Long userId,Long dailyId){
        JSONResult result = new JSONResult();
        try {
            userService.deleteRelationWithDailyId(userId,dailyId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }
}
